<?php
namespace Home\Controller;

use Think\Controller;

class RegController extends Controller
{
    public function index()
    {

        $question=D('Verify')->getoneverify();
        $this->assign('question',$question);
        $this->display();
    }


    public function checkdata(){
       // {"email":"fsdf@df.com","username":"dsf","pass":"dsfsdf","repass":"dsfsdf","vercode":"dsfdsf"}
        $pass=I('post.pass');//密码
        $repass=I('post.repass');//确认密码
        $answer=$_SESSION['answer'];  //数据库里面提取的答案
        $panswer=I('post.vercode'); //用户post过来的答案
        $email=I('post.email');
        $name=I('post.username');
        if($pass != $repass){
            $arr=array(
                "error"=>1,
                "info"=>"密码不一致"
            );
            echo json_encode($arr);
            exit();
        }
//        var_dump($answer." ".$panswer);
        if($answer != $panswer){
            $arr=array(
                "error"=>1,
                "info"=>"验证码错误啦！"
            );
            echo json_encode($arr);
            exit();
        }

        if(D('User')->checkEmail($email)){
            $arr=array(
                "error"=>1,
                "info"=>"邮箱存在啦！"
            );
            echo json_encode($arr);
            exit();

        }

        if(D('User')->checknickname($name)){
            $arr=array(
                "error"=>1,
                "info"=>"昵称存在啦！"
            );
            echo json_encode($arr);
            exit();
        }

        $arr['nickname']=$name;
        $arr['email']=$email;
        $arr['password']=md5($pass);
        $arr['create_time']=time();
        if( D('User')->add($arr)){
            $arr=array(
                "error"=>0,
                "info"=>"注册成功啦！"
            );
            echo json_encode($arr);
            exit();
        }else{
            $arr=array(
                "error"=>1,
                "info"=>"注册失败啦！"
            );
            echo json_encode($arr);
            exit();
        }


    }
}